package com.cyandpos.pad.presentation;

import android.app.Presentation;
import android.content.Context;
import android.hardware.display.DisplayManager;
import android.os.Handler;
import android.view.Display;
import android.view.Window;

import com.cyandpos.pad.R;
import com.cyandpos.pad.activity.LoginActivity;
import com.cyandpos.pad.posdata.Goods;
import com.cyandpos.pad.posdata.Member;
import com.cyandpos.pad.posdata.Payment;
import com.cyandpos.pad.posdata.TicketInfo;

import java.util.List;

public class AdPresentationUtil implements AdPresentationInterface{
    public  DisplayManager mDisplayManager;//屏幕管理类
    public  Display mDisplay[];//屏幕数组
    public  Presentation lastDisplayPresentation;//用于记录上次展示的副屏的界面
    public  AdPresentation mPresentation;

    @Override
    public  void initDisplayData(final Context context) {
        mDisplayManager = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
        mDisplay = mDisplayManager.getDisplays();
        showAdvDisplay(context);
    }
    public  void showAdvDisplay(Context context) {
        if (mDisplay.length < 2) {
            return;
        }

         mPresentation = new AdPresentation(context, mDisplay[1]);
        showPersentation(mPresentation);
    }

    @Override
    public void addMember(Member member) {
        if(mPresentation!=null)
        mPresentation.addMember(member);
    }

    @Override
    public void addGoods(List<Goods> list) {
        if(mPresentation!=null)
        mPresentation.setGoodsList(list);
    }

    @Override
    public void setCalculatorPayed(TicketInfo ticket) {
        if(mPresentation!=null)
        mPresentation.CalculatorPayed(ticket);
    }

    public  void showPersentation(final Presentation persentation) {
        persentation.show();
        //延时取消，否则会闪动
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (lastDisplayPresentation != null) {
                    lastDisplayPresentation.dismiss();
                    lastDisplayPresentation = null;
                }
                lastDisplayPresentation = persentation;
            }
        }, 100);
    }

    public  void dismiss(){
        if (lastDisplayPresentation != null) {
            lastDisplayPresentation.dismiss();
            if(mPresentation!=null) {
                mPresentation.destory();
                mPresentation.destoryTwo();
            }
        }
    }



    public  void setMode(int mode){
        if(mPresentation!=null)
        mPresentation.setMode(mode);
    }

    @Override
    public int getMode() {
        if(mPresentation!=null) {
            return mPresentation.getMode();
        }
        return 0;
    }

    @Override
    public void setData(TicketInfo ticket) {
        if(mPresentation!=null)
        mPresentation.setData(ticket);
    }

    @Override
    public void setPayment(List<Payment> paymentList) {
        if(mPresentation!=null)
        mPresentation.setPayment(paymentList);
    }
}
